package com.example.androidlearning2200;

import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.example.androidlearning2200.frg.ContactFragment;
import com.example.androidlearning2200.frg.FindFragment;
import com.example.androidlearning2200.frg.MeFragment;
import com.example.androidlearning2200.frg.MsgFragment;
import com.google.android.material.navigation.NavigationView;

public class DrawerActivity extends AppCompatActivity {

    private DrawerLayout drawerLayout;
    private Toolbar toolbar;
    private NavigationView navigationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drawer);
        drawerLayout = findViewById(R.id.drawerLayout);
        toolbar = findViewById(R.id.toolbar);
        navigationView = findViewById(R.id.navigationView);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu_white);
        navigationView.setCheckedItem(R.id.navigation_msg);
        // 初始化首个Fragment
        Fragment msgFragment = new MsgFragment();
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, msgFragment).commit();
        // 设置导航点击监听器
        navigationView.setNavigationItemSelectedListener(item -> {
            // 处理导航项点击事件
            selectDrawerItem(item);
            return true;
        });
    }

    /**
     * 响应抽屉菜单点击，切换内容
     *
     * @param item
     */
    private void selectDrawerItem(MenuItem item) {
        // 创建对应用户选择的导航项的Fragment
        Fragment fragment = null;
        switch (item.getItemId()) {
            case R.id.navigation_msg:
                fragment = new MsgFragment();
                break;
            case R.id.navigation_contact:
                fragment = new ContactFragment();
                break;
            case R.id.navigation_find:
                fragment = new FindFragment();
                break;
            case R.id.navigation_me:
                fragment = new MeFragment();
                break;
            default:
                break;
        }
        if (fragment != null) {
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragment).commit();
            item.setChecked(true);
            setTitle(item.getTitle());
            drawerLayout.closeDrawers();
        }
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                drawerLayout.openDrawer(GravityCompat.START);
                break;
        }
        return true;
    }
}